import QWebChannel from '@/views/modules/webModu/terminal/js/qwebchannel'


export function output(message) {
	console.log(message)
}
export function getJsonFromInfo(info) {
	if (info == "") {
		return "{}";
	}
	var info_list = info.split("&");
	var info_json = "{";
	for (var i = 0; i < info_list.length; i++) {
		var key_value_list = info_list[i].split(":");
		var key = key_value_list[0];
		var value = key_value_list[1];
		if (i == 0) {
			info_json += "\"" + key + "\"" + ":" + "\"" + value + "\"";
		} else {
			info_json += ",\"" + key + "\"" + ":" + "\"" + value + "\"";
		}
	}
	info_json += "}";
	return info_json;
}
export function isEmptyObject(obj) {
	for (var key in obj) {
		return false;
	}
	return true;
}
export let coinhopper = {
	coinReturnJobCompleteCB: function () { },
	coinhopperConnectedCB: function () { },
	//munconnected:function(){},
	connected: function (callback) {
		coinhopperConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				coinhopperobject = channel.objects.coinhopper;
				coinhopperobject.coinreturncompleted.connect(function (return_code, job_id, info) {
					coinReturnJobCompleteCB(return_code, info);
				});
				if (typeof (coinhopperConnectedCB) != "undefined") {
					coinhopperConnectedCB();
				}
			});
		};
		//socket.onclose = function(){
		//	callback("onclose");
		//};
		//socket.onerror = function(error){
		//	callback(error);
		//};
	},
	open: function (callback) {
		coinhopperobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		coinhopperobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		coinhopperobject.getstatus(function (state_info) {
			if (isEmptyObject(state_info)) {
				callback("Exception", "207");
			} else {
				var state_info_str = state_info.toString().split(",");
				callback(state_info_str[0], state_info_str[1]);
			}
		});
	},
	getInfo: function (callback) {
		coinhopperobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		coinhopperobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	coinReturn: function (options, num, callback) {
		coinhopperobject.coinreturn(JSON.stringify(options), num, function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	coinReturnJobCompleted: function (callback) {
		coinReturnJobCompleteCB = callback;
	}
}
export let coindeposit = {
	startReceiveCompletedCB: function () { },
	coindepositConnectedCB: function () { },
	connected: function (callback) {
		coindepositConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				depositobject = channel.objects.coindeposit;
				depositobject.coincomed.connect(function (return_code, job_id, info) {
					startReceiveCompletedCB(return_code, info);
				});
				if (typeof (coindepositConnectedCB) != "undefined") {
					coindepositConnectedCB();
				}
			});
		};
		//socket.onclose = unconnected;
		//socket.onerror = function(error){
		//	output("socket connect error " + error);
		//};
	},
	open: function (callback) {
		depositobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		depositobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		depositobject.getstatus(function (state_info) {
			if (isEmptyObject(state_info)) {
				callback("Exception", "207");
			} else {
				var state_info_str = state_info.toString().split(",");
				callback(state_info_str[0], state_info_str[1]);
			}
		});
	},
	getInfo: function (callback) {
		depositobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		depositobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	startReceive: function (options, callback) {
		depositobject.startrecv(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	stopReceive: function (callback) {
		depositobject.stoprecv(function (return_code) {
			callback(return_code.toString());
		});
	},
	getCashInfo: function (callback) {
		depositobject.getcashinfo(function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0], result_str[1]);
		});
	},
	pullCash: function (callback) {
		depositobject.pullcash(function (return_code) {
			callback(return_code.toString());
		});
	},
	pushCash: function (callback) {
		depositobject.pushcash(function (return_code) {
			callback(return_code.toString());
		});
	},
	initDevice: function (callback) {
		depositobject.initdevice(function (return_code) {
			callback(return_code.toString());
		});
	},
	startReceiveCompleted: function (callback) {
		startReceiveCompletedCB = callback;
	}
}
export let nfccardreader = {
	getCardInfoCompletedCB: function () { },
	nfcConnectedCB: function () { },
	cardComedCB: function () { },
	cardLeavedCB: function () { },
	connected: function (callback) {
		nfcConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			//output("socket opened");
			new QWebChannel(socket, function (channel) {
				nfccardreaderobject = channel.objects.nfcreader;
				nfccardreaderobject.scannfccardcompleted.connect(function (return_code, job_id, info) {
					getCardInfoCompletedCB(return_code, info);
				});
				nfccardreaderobject.cardcomed.connect(function (info) {
					if (typeof (cardComedCB) != "undefined") {
						cardComedCB(info);
					}
				});
				nfccardreaderobject.cardleaved.connect(function (info, period) {
					if (typeof (cardLeavedCB) != "undefined") {
						cardLeavedCB(info, period);
					}
				});
				if (typeof (nfcConnectedCB) != "undefined") {
					nfcConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		nfccardreaderobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		nfccardreaderobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		nfccardreaderobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		nfccardreaderobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options, callback) {
		nfccardreaderobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	resetFromException: function (devid, callback) {
		nfccardreaderobject.resetfromexception(devid, function (return_code) {
			callback(return_code.toString());
		})
	},
	readCardInfo: function (options, callback) {
		nfccardreaderobject.scannfccard(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	readCardInfoCompleted: function (callback) {
		getCardInfoCompletedCB = callback;
		//callback(result,info);
		//nfccardreaderobject.scannfccardcompleted.connect(function(return_code,job_id,info){
		//	callback(return_code,info);	
		//});
	},
	cardComed: function (callback) {
		cardComedCB = callback;
	},
	cardLeaved: function (callback) {
		cardLeavedCB = callback;
	},
	checkCard: function (callback) {
		nfccardreaderobject.cardcheck(function (return_code) {
			callback(return_code.toString());
		});
	},
	resetDevice: function (callback) {
		nfccardreaderobject.resetdevice(function (return_code) {
			callback(return_code.toString());
		});
	}
}
export let iccardreader = {
	insertCardJobCompletedCB: function () { },
	icConnectedCB: function () { },
	connected: function (callback) {
		icConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				icreaderobject = channel.objects.motorizedcardreader;
				icreaderobject.insertcardcompleted.connect(function (return_code, job_id, result) {
					insertCardJobCompletedCB(return_code);
				});
				if (typeof (icConnectedCB) != "undefined") {
					icConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		icreaderobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		icreaderobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		icreaderobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		icreaderobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		icreaderobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	insertCard: function (options, callback) {
		icreaderobject.insertcard(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	stopInsertCard: function (callback) {
		icreaderobject.stopasyjob(function (return_code) {
			callback(return_code.toString());
		});
	},
	ejectCard: function (callback) {
		icreaderobject.ejectcard(function (return_code) {
			callback(return_code.toString());
		});
	},
	captureCard: function (callback) {
		icreaderobject.capturecard(function (return_code) {
			callback(return_code.toString());
		});
	},
	insertCardJobCompleted: function (callback) {
		insertCardJobCompletedCB = callback;
	}
}
export let passwordkb = {
	getPressedButtonCompletedCB: function () { },
	pkbConnectedCB: function () { },
	connected: function (callback) {
		pkbConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				passwordkbobject = channel.objects.codekeyboard;
				passwordkbobject.getpressedbuttoncompleted.connect(function (return_code, job_id, key_value) {
					getPressedButtonCompletedCB(return_code, key_value);
				});
				if (typeof (pkbConnectedCB) != "undefined") {
					pkbConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		passwordkbobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		passwordkbobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		passwordkbobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		passwordkbobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		passwordkbobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	getPressedButton: function (options, callback) {
		passwordkbobject.getpressedbutton(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	getPressedButtonCompleted: function (callback) {
		getPressedButtonCompletedCB = callback;
	},
	stopGetPressedButton: function (callback) {
		passwordkbobject.stopasyjob(function (return_code) {
			callback(return_code.toString());
		});
	}
}
export let ticketprinter = {
	ticketConnectedCB: function () { },
	printCompletedCB: function () { },
	connected: function (callback) {
		ticketConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				ticketobject = channel.objects.ticketprinter;
				ticketobject.printcompleted.connect(function (return_code, job_id, info) {
					printCompletedCB(return_code, job_id);
				});
				if (typeof (ticketConnectedCB) != "undefined") {
					ticketConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		ticketobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		ticketobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		ticketobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		ticketobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		ticketobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	cutOff: function (callback) {
		ticketobject.cutoff(function (return_code) {
			callback(return_code.toString());
		});
	},
	printString: function (options, str, callback) {
		ticketobject.printstring(JSON.stringify(options), str, function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	printBarcode: function (options, str, callback) {
		ticketobject.printbarcode(JSON.stringify(options), str, function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	printQrcode: function (options, str, callback) {
		ticketobject.printqrcode(JSON.stringify(options), str, function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	printBmpImage: function (options, path, callback) {
		ticketobject.printimg(JSON.stringify(options), path, function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	printCompleted: function (callback) {
		printCompletedCB = callback;
	}
}
export let barcodereader = {
	startScanCompletedCB: function () { },
	brConnectedCB: function () { },
	connected: function (callback) {
		brConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			//output("socket opened");
			new QWebChannel(socket, function (channel) {
				barcodereaderobject = channel.objects.barcodereader;
				barcodereaderobject.scanbarcodecompleted.connect(function (return_code, job_id, info) {
					startScanCompletedCB(return_code, info);
				});
				if (typeof (brConnectedCB) != "undefined") {
					brConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		barcodereaderobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		barcodereaderobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		barcodereaderobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		barcodereaderobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		barcodereaderobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	startScan: function (options, callback) {
		barcodereaderobject.scanbarcode(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	startScanCompleted: function (callback) {
		startScanCompletedCB = callback;
	}
}
export let fingerreader = {
	startScanCompletedCB: function () { },
	fingerConnectedCB: function () { },
	starRecordFinger: function () { },
	fingerinfo: function () { },
	countOld: -1,
	//var countOld = -1;

	connected: function (callback) {
		fingerConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			//output("socket opened");
			new QWebChannel(socket, function (channel) {
				fingereaderobject = channel.objects.fingerprintinst;
				fingereaderobject.scanfingerprintcompleted.connect(function (return_code, job_id, info) {
					fingerreader.startScanCompletedCB(return_code, info);
				});
				fingereaderobject.fingerinfo.connect(function (count, status) {
					//console.log(count + "-------------" + status);
					//							starRecordFinger(count, status);
					if (countOld != count) {
						countOld = count;

						fingerreader.starRecordFinger(count, status);
					}
				});
				fingereaderobject.normalinfo.connect(function (infoType, info, infoLen) {
					fingerreader.fingerinfo(infoType, info, infoLen);
				});

				if (typeof (fingerConnectedCB) != "undefined") {
					fingerConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		fingereaderobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		fingereaderobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		fingereaderobject.getstatus(function (state_info) {
			// var state_info_str = state_info.toString().split(",");
			callback(state_info[0], state_info[1]);
		});
	},
	getInfo: function (callback) {
		fingereaderobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		fingereaderobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	startScan: function (options, callback) {
		fingereaderobject.scanfingerprint(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	startScanCompleted: function (callback) {
		startScanCompletedCB = callback;
	},
	handShake: function (callback) {
		fingereaderobject.handshake(function (return_code) {
			callback(return_code.toString());
		});
	},
	identifyFinger: function (callback) {
		fingereaderobject.identifyfinger(function (return_code) {
			callback(return_code.toString());
		});
	},
	resetDevice: function (callback) {
		fingereaderobject.resetdevice(function (return_code) {
			callback(return_code.toString());
		});
	},
	checkMem: function (callback) {
		fingereaderobject.checkmem(function (meminfo) {
			callback(meminfo);
			console.log(meminfo);
			//var info_str = info.toString().split(",");
			//var handwareInfoJson = getJsonFromInfo(info_str[0]);
			//callback(return_code.toString());
		});
	},
	recordFinger: function (userNumber, fingerNumber, callback) {
		fingereaderobject.recordfinger(userNumber, fingerNumber, function (return_code) {
			countOld = -1;
			callback(return_code.toString());
		});
	},
	checkUser: function (userNumber, fingerNumber, callback) {
		fingereaderobject.checkuser(userNumber, fingerNumber, function (return_code) {
			console.log(return_code);

			callback(return_code);
		});
	},
	delUser: function (userNumber, fingerNumber, callback) {
		fingereaderobject.deluser(userNumber, fingerNumber, function (return_code) {
			console.log(return_code.toString());
			callback(return_code.toString());
		});
	},

	delAllUser: function (callback) {
		var userNumber = "########";
		var fingerNumber = 1;
		fingereaderobject.deluser(userNumber, fingerNumber, function (return_code) {
			callback(return_code.toString());
		});
	},
	readFeature: function (userNumber, fingerNumber, callback) {
		if (userNumber == null) {
			console.log("readFeature第一个传参不能为空");
			return;
		}
		if (fingerNumber == null) {
			console.log("readFeature第二个传参不能为空");
			return;
		}
		fingereaderobject.readfeature(userNumber, fingerNumber, function (return_code) {
			// console.log(return_code);
			callback(return_code);
		});
	},
	writeFeature: function (userNumber, fingerNumber, fingerFeature, featureLen, callback) {
		fingereaderobject.writefeature(userNumber, fingerNumber, fingerFeature, featureLen, function (return_code) {
			callback(return_code.toString());
		});
	},
	getfingerinfo: function (callback) {
		this.fingerinfo = callback;
	},
	getrecordfingerinfo: function (callback) {
		this.starRecordFinger = callback;
	}
}
export let idcardreader = {
	startScanCompletedCB: function () { },
	idConnectedCB: function () { },
	connected: function (callback) {
		this.idConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			//output("socket opened");
			new QWebChannel(socket, function (channel) {
				idcardreaderobject = channel.objects.idreader;
				idcardreaderobject.scanidcardcompleted.connect(function (return_code, job_id, info) {
					this.startScanCompletedCB(return_code, info);
				});
				if (typeof (this.idConnectedCB) != "undefined") {
					this.idConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		idcardreaderobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		idcardreaderobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		idcardreaderobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		idcardreaderobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		idcardreaderobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	startScan: function (options, callback) {
		idcardreaderobject.scanidcard(JSON.stringify(options), function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	startScanCompleted: function (callback) {
		this.startScanCompletedCB = callback;
	}
}
export let virtualkeyboard = {
	vkConnectedCB: function () { },
	connected: function (callback) {
		vkConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			//output("socket opened");
			new QWebChannel(socket, function (channel) {
				onboardobject = channel.objects.virtualkeyboard;
				if (typeof (vkConnectedCB) != "undefined") {
					vkConnectedCB();
				}
			});
		};
	},
	hide: function () {
		onboardobject.hide();
	},
	show: function () {
		onboardobject.show();
	},
	setDefaultOptions: function (options) {
		onboardobject.setoptions(JSON.stringify(options));
	}
}
export let labelprinter = {
	printCompletedCB: function () { },
	labelConnectedCB: function () { },
	connected: function (callback) {
		labelConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				labeltobject = channel.objects.labelprinter;
				labeltobject.printcompleted.connect(function (return_code, job_id, info) {
					printCompletedCB(return_code, job_id);
				});
				if (typeof (labelConnectedCB) != "undefined") {
					labelConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		labeltobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		labeltobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		labeltobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		labeltobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		labeltobject.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	cutOff: function (callback) {
		labeltobject.cutoff(function (return_code) {
			callback(return_code.toString());
		});
	},
	printString: function (options, str, callback) {
		labeltobject.printstring(JSON.stringify(options), str, function (result) {
			var result_str = result.toString().split(",");
			callback(result_str[0]);
		});
	},
	//printBmpImage:function(options,path,callback){
	//	labeltobject.printimg(JSON.stringify(options),path,function(result){
	//		var result_str = result.toString().split(",");
	//		callback(result_str[0]);
	//	});
	//},
	printCompleted: function (callback) {
		printCompletedCB = callback;
	}
}
export let camera = {
	takePhotoCompletedCB: function () { },
	cameraConnectedCB: function () { },
	connected: function (callback) {
		cameraConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				hgpobject = channel.objects.highgraphapparatus;
				hgpobject.previewres.connect(function (result, base64str) {
					takePhotoCompletedCB(result, base64str);
				});
				if (typeof (cameraConnectedCB) != "undefined") {
					cameraConnectedCB();
				}
			});
		};
	},
	takePhoto: function (options, callback) {
		hgpobject.startpreviewex(JSON.stringify(options), callback);
	},
	takePhotoCompleted: function (callback) {
		takePhotoCompletedCB = callback;
	}
}
//A4打印机
export let commonprinter = {
	printCompletedCB: function () { },
	commonprinterConnectedCB: function () { },
	connected: function (callback) {
		commonprinterConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				printobject = channel.objects.commonprinter;
				if (typeof (commonprinterConnectedCB) != "undefined") {
					commonprinterConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		printobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		printobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		printobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		printobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	printFile: function (options, path, callback) {
		printobject.startprint(JSON.stringify(options), path, function (result) { });
	},
	printCompleted: function (callback) {
		printCompletedCB = callback;
	},
	printHtml: function () {
		window.print();
	},
	uploadFile: function (callback, deviceid) {
		var filed = document.querySelector("input[type=file]").files[0];
		if (filed === undefined) {
			//output("Printing cached file");
			printobject.printFile(callback, deviceid);
			return;
		}
		var filename = filed.name;
		var filesize = filed.size;
		//output("file name : " + filename);
		//output("file size : " + filesize);

		var reader = new FileReader();
		var rawData = new ArrayBuffer();
		reader.onload = function (fd) {
			rawData = fd.target.result;
			tmpdata = new Uint8Array(rawData);
			//var data = String.fromCharCode.apply(null, tmpdata);
			var data = Array.from(tmpdata);
			printobject.uploadfile(data);
		}

		reader.loadend = function (e) { }

		reader.onerror = function (e) {
			//output("file uploaded failed");
		}

		//reader.readAsBinaryString(filed);
		reader.readAsArrayBuffer(filed);
	}
}
// 照片打印机
export let photoprinter = {
	printCompleted: function () { },
	photoprinterConnectedCB: function () { },
	connected: function (callback) {
		console.log("connectting")
		photoprinterConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onclose = function () {
			console.error("web channel close");
		};
		socket.onerror = function (error) {
			console.error("web channel error: " + error);
		}
		socket.onopen = function () {
			console.log("web channel init success.")
			new QWebChannel(socket, function (channel) {
				content = channel.objects.photoprinter;
				content.printcompleted.connect(function (return_code, job_id, info) {
					printCompletedCB(return_code, job_id, info)
				});
				if (typeof (photoprinterConnectedCB) != "undefined") {
					photoprinterConnectedCB();
				}
			})
		}
	},
	open: function (callback) {
		content.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		content.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		content.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		content.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	resetFromException: function (devid, callback) {
		content.resetfromexception(devid, function (return_code) {
			callback(return_code.toString());
		})
	},
	setDefaultOptions: function (options) {
		content.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	printFile: function (path, file_name, callback) {
		content.printfile(path, file_name, function (return_code) {
			callback(return_code.toString());
		});
	},
	clear: function (callback) {
		content.cleanprinthead(function (return_code) {
			callback(return_code.toString())
		})
	},
	cancelAllJob: function (callback) {
		content.cancelalljob(function (return_code) {
			callback(return_code.toString())
		})
	},
	printCompleted: function (callback) {
		printCompletedCB = callback;
	}
}
export let facecamera = {
	takePhotoCompletedCB: function () { },
	facecameraConnectedCB: function () { },
	connected: function (callback) {
		facecameraConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				faceobject = channel.objects.facerecognition;
				faceobject.sendImag.connect(function (base64str) {
					document.getElementById("face").src = "/tmp/visible_110000198010101010.jpg" + "?t=" + Math.random();
				});
				if (typeof (facecameraConnectedCB) != "undefined") {
					facecameraConnectedCB();
				}
			});
		};
	},
	startgtk: function () {
		faceobject.startgtk();
	},
	startrecognition: function () {
		faceobject.startrecognition();
	},
	stoprecognition: function () {
		faceobject.stoprecognition();
	},
	receivesignals: function () {
		faceobject.receivesignals()
	}
}
// UPS
export let ups = {
	jobOutCompleted: function () { },
	upsConnectedCompleted: function () { },
	connected: function (callback) {
		upsConnectedCompleted = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onclose = function () {
			console.error("web channel close");
		};
		socket.onerror = function (error) {
			console.error("web channel error: " + error);
		}
		socket.onopen = function () {
			console.log("web channel init success.")
			new QWebChannel(socket, function (channel) {
				content = channel.objects.photoprinter;
				content.execjobcompleted.connect(function (return_code, job_id, info) {
					jobOutCompleted(return_code, job_id, info)
				});
				if (typeof (upsConnectedCompleted) != "undefined") {
					upsConnectedCompleted();
				}
			})
		}
	},
	open: function (callback) {
		content.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		content.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		content.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		content.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	resetFromException: function (devid, callback) {
		content.resetfromexception(devid, function (return_code) {
			callback(return_code.toString());
		})
	},
	setDefaultOptions: function (options) {
		content.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	conrolUps: function (cmd, power, power_on) {
		content.controlups(cmd, power_off, power_on, function (return_code) {
			callback(return_code)
		})
	},
	execjobCompleted: function (callback) {
		jobOutCompleted = callback;
	}
}
export let microphone = {
	printCompletedCB: function () { },
	micConnectedCB: function () { },
	connected: function (callback) {
		micConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onopen = function () {
			new QWebChannel(socket, function (channel) {
				microphoneobject = channel.objects.microphone;
				microphoneobject.printcompleted.connect(function (return_code, job_id, info) {
					printCompletedCB(return_code, job_id);
				});
				if (typeof (labelConnectedCB) != "undefined") {
					micConnectedCB();
				}
			});
		};
	},
	open: function (callback) {
		microphoneobject.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		microphoneobject.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		microphoneobject.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		microphoneobject.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	startRecord: function (path, second, callback) {
		microphoneobject.startrecord(path, second, function (return_code) {
			callback(return_code.toString())
		})
	},
	stopRecord: function (callback) {
		microphoneobject.stoprecord(function (return_code) {
			callback(return_code.toString())
		})
	},
	getSoundDirection: function (callback) {
		microphoneobject.getsounddirection(function (status) {
			callback(status[0], status[1])
		})
	},
	checkRecording: function (callback) {
		microphoneobject.checkrecording(function (return_code) {
			callback(return_code.toString())
		})
	},
	printCompleted: function (callback) {
		printCompletedCB = callback;
	},
	captureMicrophone: function (callback) {

		if (microphone) {
			callback(microphone);
			return;
		}

		if (typeof navigator.mediaDevices === 'undefined' || !navigator.mediaDevices.getUserMedia) {
			alert('This browser does not supports WebRTC getUserMedia API.');

			if (!!navigator.getUserMedia) {
				alert('This browser seems supporting deprecated getUserMedia API.');
			}
		}

		navigator.mediaDevices.getUserMedia({
			audio: isEdge ? true : {
				echoCancellation: false
			}
		}).then(function (mic) {
			callback(mic);
		}).catch(function (error) {
			alert('Unable to capture your microphone. Please check console logs.');
			console.error(error);
		});
	},
	replaceAudio: function (src) {
		var newAudio = document.createElement('audio');
		newAudio.controls = true;
		newAudio.autoplay = true;

		if (src) {
			newAudio.src = src;
		}

		var parentNode = audio.parentNode;
		parentNode.innerHTML = '';
		parentNode.appendChild(newAudio);

		audio = newAudio;
	},
	stopRecordingCallback: function () {
		replaceAudio(URL.createObjectURL(recorder.getBlob()));

		btnStartRecording.disabled = false;

		setTimeout(function () {
			if (!audio.paused) return;

			setTimeout(function () {
				if (!audio.paused) return;
				audio.play();
			}, 1000);

			audio.play();
		}, 300);

		audio.play();

		if (isSafari) {
			click(btnReleaseMicrophone);
		}
	}
}
export let webfacecamera = {
	facepassCompletedCB: function () { },
	facenopassedCompletedCB: function () { },
	connected: function (callback) {
		console.log("connectting")
		this.facecameraConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onclose = function () {
			console.error("web channel close");
		};
		socket.onerror = function (error) {
			console.error("web channel error: " + error);
		}
		socket.onopen = function () {
			console.log("web channel init success.")
			new QWebChannel(socket, function (channel) {
				content = channel.objects.facecamera;
				content.facepassed.connect(function (photo1, photo2, photo3) {
					this.facepassCompletedCB(photo1, photo2, photo3)
				});
				content.facenopassed.connect(function (return_code) {
					this.facenopassedCompletedCB(return_code)
				});
				if (typeof (facecameraConnectedCB) != "undefined") {
					this.facecameraConnectedCB();
				}
			})
		}
	},
	open: function (callback) {
		content.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		content.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		content.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		content.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	setDefaultOptions: function (options) {
		content.setdefaultoptions(JSON.stringify(options), function (return_code) {
			callback(return_code.toString());
		});
	},
	startCapture: function (callback) {
		content.startcapture('{}', function (return_code, job_id) {
			if (!job_id) { job_id = 0; }
			callback(return_code.toString(), job_id.toString());
		})
	},
	stopCapture: function (callback) {
		content.stopcapture(function (return_code) {
			callback(return_code.toString());
		})
	},
	resetDevice: function (callback) {
		content.resetdevice(function (return_code) {
			callback(return_code.toString());
		})
	},
	facenopassedCompleted: function (callback) {
		this.facenopassedCompletedCB = callback
	},
	facepassCompleted: function (callback) {
		this.facepassCompletedCB = callback
	}
}
export let iocontroler = {
	iocontrolerConnectedCB: function () { },
	connected: function (callback) {
		console.log("connectting")
		iocontrolerConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onclose = function () {
			console.error("web channel close");
		};
		socket.onerror = function (error) {
			console.error("web channel error: " + error);
		}
		socket.onopen = function () {
			console.log("web channel init success.")
			new QWebChannel(socket, function (channel) {
				content = channel.objects.iocontroler;
				if (typeof (iocontrolerConnectedCB) != "undefined") {
					iocontrolerConnectedCB();
				}
			})
		}
	},
	open: function (callback) {
		content.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		content.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		content.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		content.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	writeGpIo: function (gpio_num, action, callback) {
		content.writegpio(gpio_num, action, function (return_code) {
			callback(return_code)
		})
	}
}
export let humanInduction = {
	bodyOnCB: function () { },
	bodyOffCB: function () { },
	body: function (a) { },
	connected: function (callback) {
		console.log("connectting")
		iocontrolerConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onclose = function () {
			console.error("web channel close");
		};
		socket.onerror = function (error) {
			console.error("web channel error: " + error);
		}
		socket.onopen = function () {
			console.log("web channel init success.")
			new QWebChannel(socket, function (channel) {
				content = channel.objects.humaninduction;
				content.bodyon.connect(function () {
					body(0)
				});
				content.bodyoff.connect(function () {
					body(1)
				});
				if (typeof (iocontrolerConnectedCB) != "undefined") {
					iocontrolerConnectedCB();
				}
			})
		}
	},
	open: function (callback) {
		content.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		content.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	getState: function (callback) {
		content.getstatus(function (state_info) {
			var state_info_str = state_info.toString().split(",");
			callback(state_info_str[0], state_info_str[1]);
		});
	},
	getInfo: function (callback) {
		content.getinfo(function (info) {
			var info_str = info.toString().split(",");
			var handwareInfoJson = getJsonFromInfo(info_str[0]);
			var deviceStateInfoJson = getJsonFromInfo(info_str[1]);
			var staticInfoJson = getJsonFromInfo(info_str[2]);
			callback(handwareInfoJson, deviceStateInfoJson, staticInfoJson);
		});
	},
	bodyOff: function (callback) {
		bodyOffCB = callback
	},
	bodyOn: function (callback) {
		bodyOnCB = callback
	},
	bodyCB: function (callback) {
		body = callback
	}
}
export let textinput = {
	connected: function (callback) {
		console.log("connectting")
		textinputConnectedCB = callback;
		var socket = new WebSocket("ws://localhost:12345");
		socket.onclose = function () {
			console.error("web channel close");
		};
		socket.onerror = function (error) {
			console.error("web channel error: " + error);
		}
		socket.onopen = function () {
			console.log("web channel init success.")
			new QWebChannel(socket, function (channel) {
				content = channel.objects.textinput;
				if (typeof (textinputConnectedCB) != "undefined") {
					textinputConnectedCB();
				}
			})
		}
	},
	open: function (callback) {
		content.open(function (return_code) {
			callback(return_code.toString());
		});
	},
	close: function (callback) {
		content.close(function (return_code) {
			callback(return_code.toString());
		});
	},
	showKeyBoard: function (status, callback) {
		content.showkeyboard(status, function (return_code) {
			callback(return_code.toString());
		})
	},
	moveKeyBoard: function (x, y, callback) {
		content.movekeyboard(x, y, function (return_code) {
			callback(return_code.toString());
		});
	}
}

//facecamera.connected();
//camera.connected();

